package com.mm.cloud.lease.service.impl.convert.income;

import com.mm.cloud.common.security.loginuser.LoginUser;
import com.mm.cloud.lease.entity.Tenant;
import com.mm.cloud.lease.entity.TenantIncome;
import com.mm.cloud.lease.entity.TenantOtherFee;
import com.mm.cloud.lease.entity.TenantSubIncome;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 租客账单 构建转换类
 * 说明：不使用 BeanUtils，显式赋值；集中构造便于维护与复用
 */
public final class TenantIncomeConvert {

    private TenantIncomeConvert() {}

    public static TenantIncome buildIncome(LoginUser user, Tenant tenant,
                                           LocalDateTime start, LocalDateTime end,
                                           Integer cycleMonths, Integer paySeq,
                                           LocalDateTime payDate, BigDecimal totalAmount) {
        TenantIncome income = new TenantIncome();
        income.setCompanyId(user.getCompanyId());
        income.setHousingId(tenant.getHousingId());
        income.setRoomId(tenant.getRoomId());
        income.setTenantId(tenant.getId());
        income.setRenewId(null);
        income.setLogIndex(0);
        income.setStartValidDate(start);
        income.setEndValidDate(end);
        income.setValidYear(0);
        income.setValidMonth(cycleMonths);
        income.setValidDay(0);
        income.setPayCount(paySeq);
        income.setPayDate(payDate);
        income.setPayMoney(totalAmount);
        income.setDebtMoney(totalAmount);
        income.setPayStatus((byte) 1);
        income.setFinanceMoney(BigDecimal.ZERO);
        income.setUnfinanceMoney(totalAmount);
        income.setLateFeeMoney(BigDecimal.ZERO);
        income.setBreakLateFeeMoney(BigDecimal.ZERO);
        income.setIsBadDebt((byte) 1);
        income.setIsSendSms((byte) 1);
        income.setCreaterId(user.getUserId());
        return income;
    }

    public static TenantIncome buildIncome(LoginUser user, Long housingId, Long roomId, Long tenantId,
                                           LocalDateTime start, LocalDateTime end,
                                           Integer cycleMonths, Integer paySeq,
                                           LocalDateTime payDate, BigDecimal totalAmount) {
        TenantIncome income = new TenantIncome();
        income.setCompanyId(user.getCompanyId());
        income.setHousingId(housingId);
        income.setRoomId(roomId);
        income.setTenantId(tenantId);
        income.setRenewId(null);
        income.setLogIndex(0);
        income.setStartValidDate(start);
        income.setEndValidDate(end);
        income.setValidYear(0);
        income.setValidMonth(cycleMonths);
        income.setValidDay(0);
        income.setPayCount(paySeq);
        income.setPayDate(payDate);
        income.setPayMoney(totalAmount);
        income.setDebtMoney(totalAmount);
        income.setPayStatus((byte) 1);
        income.setFinanceMoney(BigDecimal.ZERO);
        income.setUnfinanceMoney(totalAmount);
        income.setLateFeeMoney(BigDecimal.ZERO);
        income.setBreakLateFeeMoney(BigDecimal.ZERO);
        income.setIsBadDebt((byte) 1);
        income.setIsSendSms((byte) 1);
        income.setCreaterId(user.getUserId());
        return income;
    }

    public static TenantSubIncome buildRentSub(LoginUser user, Tenant tenant,
                                               LocalDateTime start, LocalDateTime end,
                                               Integer paySeq, LocalDateTime payDate, BigDecimal amount) {
        TenantSubIncome sub = baseSub(user, tenant, start, end, paySeq, payDate);
        sub.setSubIncomeType((byte) 1);
        sub.setPayMoney(amount);
        sub.setDebtMoney(amount);
        sub.setUnfinanceMoney(amount);
        return sub;
    }

    public static TenantSubIncome buildRentSub(LoginUser user, Long housingId, Long roomId, Long tenantId,
                                               LocalDateTime start, LocalDateTime end,
                                               Integer paySeq, LocalDateTime payDate, BigDecimal amount) {
        TenantSubIncome sub = baseSub(user, housingId, roomId, tenantId, start, end, paySeq, payDate);
        sub.setSubIncomeType((byte) 1);
        sub.setPayMoney(amount);
        sub.setDebtMoney(amount);
        sub.setUnfinanceMoney(amount);
        return sub;
    }

    public static TenantSubIncome buildDepositSub(LoginUser user, Tenant tenant,
                                                  LocalDateTime start, LocalDateTime end,
                                                  Integer paySeq, LocalDateTime payDate, BigDecimal amount) {
        TenantSubIncome sub = baseSub(user, tenant, start, end, paySeq, payDate);
        sub.setSubIncomeType((byte) 0);
        sub.setPayMoney(amount);
        sub.setDebtMoney(amount);
        sub.setUnfinanceMoney(amount);
        return sub;
    }

    public static TenantSubIncome buildDepositSub(LoginUser user, Long housingId, Long roomId, Long tenantId,
                                                  LocalDateTime start, LocalDateTime end,
                                                  Integer paySeq, LocalDateTime payDate, BigDecimal amount) {
        TenantSubIncome sub = baseSub(user, housingId, roomId, tenantId, start, end, paySeq, payDate);
        sub.setSubIncomeType((byte) 0);
        sub.setPayMoney(amount);
        sub.setDebtMoney(amount);
        sub.setUnfinanceMoney(amount);
        return sub;
    }

    public static TenantSubIncome buildOtherFeeSub(LoginUser user, Tenant tenant,
                                                   LocalDateTime start, LocalDateTime end,
                                                   Integer paySeq, LocalDateTime payDate, TenantOtherFee fee) {
        TenantSubIncome sub = baseSub(user, tenant, start, end, paySeq, payDate);
        sub.setSubIncomeType((byte) 2);
        sub.setSubIncomeDicId(fee.getDicId());
        sub.setPayMoney(fee.getFeePrice());
        sub.setDebtMoney(fee.getFeePrice());
        sub.setUnfinanceMoney(fee.getFeePrice());
        return sub;
    }

    public static TenantSubIncome buildOtherFeeSub(LoginUser user, Long housingId, Long roomId, Long tenantId,
                                                   LocalDateTime start, LocalDateTime end,
                                                   Integer paySeq, LocalDateTime payDate, Long dicId, BigDecimal amount) {
        TenantSubIncome sub = baseSub(user, housingId, roomId, tenantId, start, end, paySeq, payDate);
        sub.setSubIncomeType((byte) 2);
        sub.setSubIncomeDicId(dicId);
        sub.setPayMoney(amount);
        sub.setDebtMoney(amount);
        sub.setUnfinanceMoney(amount);
        return sub;
    }

    public static TenantSubIncome buildOtherDepositSub(LoginUser user, Tenant tenant,
                                                       LocalDateTime start, LocalDateTime end,
                                                       Integer paySeq, LocalDateTime payDate,
                                                       Long dicId, BigDecimal amount) {
        TenantSubIncome sub = baseSub(user, tenant, start, end, paySeq, payDate);
        sub.setSubIncomeType((byte) 3);
        sub.setSubIncomeDicId(dicId);
        sub.setPayMoney(amount);
        sub.setDebtMoney(amount);
        sub.setUnfinanceMoney(amount);
        return sub;
    }

    public static TenantSubIncome buildOtherDepositSub(LoginUser user, Long housingId, Long roomId, Long tenantId,
                                                       LocalDateTime start, LocalDateTime end,
                                                       Integer paySeq, LocalDateTime payDate,
                                                       Long dicId, BigDecimal amount) {
        TenantSubIncome sub = baseSub(user, housingId, roomId, tenantId, start, end, paySeq, payDate);
        sub.setSubIncomeType((byte) 3);
        sub.setSubIncomeDicId(dicId);
        sub.setPayMoney(amount);
        sub.setDebtMoney(amount);
        sub.setUnfinanceMoney(amount);
        return sub;
    }

    private static TenantSubIncome baseSub(LoginUser user, Tenant tenant,
                                           LocalDateTime start, LocalDateTime end,
                                           Integer paySeq, LocalDateTime payDate) {
        TenantSubIncome sub = new TenantSubIncome();
        sub.setCompanyId(user.getCompanyId());
        sub.setHousingId(tenant.getHousingId());
        sub.setRoomId(tenant.getRoomId());
        sub.setTenantId(tenant.getId());
        sub.setRenewId(null);
        sub.setLogIndex(0);
        sub.setStartValidDate(start);
        sub.setEndValidDate(end);
        sub.setPayCount(paySeq);
        sub.setPayDate(payDate);
        sub.setPayStatus((byte) 1);
        sub.setFinanceMoney(BigDecimal.ZERO);
        sub.setLateFeeMoney(BigDecimal.ZERO);
        sub.setBreakLateFeeMoney(BigDecimal.ZERO);
        sub.setIsBadDebt((byte) 1);
        return sub;
    }

    private static TenantSubIncome baseSub(LoginUser user, Long housingId, Long roomId, Long tenantId,
                                           LocalDateTime start, LocalDateTime end,
                                           Integer paySeq, LocalDateTime payDate) {
        TenantSubIncome sub = new TenantSubIncome();
        sub.setCompanyId(user.getCompanyId());
        sub.setHousingId(housingId);
        sub.setRoomId(roomId);
        sub.setTenantId(tenantId);
        sub.setRenewId(null);
        sub.setLogIndex(0);
        sub.setStartValidDate(start);
        sub.setEndValidDate(end);
        sub.setPayCount(paySeq);
        sub.setPayDate(payDate);
        sub.setPayStatus((byte) 1);
        sub.setFinanceMoney(BigDecimal.ZERO);
        sub.setLateFeeMoney(BigDecimal.ZERO);
        sub.setBreakLateFeeMoney(BigDecimal.ZERO);
        sub.setIsBadDebt((byte) 1);
        return sub;
    }
}


